package org.tsinghua.blockchain.client;

import hprose.client.HproseClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.tsinghua.blockchain.BlockChainException;
import org.tsinghua.blockchain.PropertiesConfigurator;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @author ipenta
 * @ClassName: BlockChainBeanRegister
 * @Description: TODO
 * @date 2017-04-28 下午3:13
 */
@Component
public class BlockChainRegister {

	@Autowired
	private PropertiesConfigurator propertiesConfigurator;

	@Bean
	public HproseClient createHproseClient() {
		HproseClient hproseClient = null;
		try {
			hproseClient = HproseClient.create(propertiesConfigurator.getUrl());
		} catch (IOException | URISyntaxException e) {
			throw new org.tsinghua.blockchain.BlockChainException(e);
		}
		return hproseClient;
	}

	@Bean
	public BlockChainClient createBlockChainProvider() {
		HproseClient hproseClient = null;
		try {
			hproseClient = HproseClient.create(propertiesConfigurator.getUrl());
		} catch (IOException | URISyntaxException e) {
			throw new BlockChainException(e);
		}
		return hproseClient.useService(BlockChainClient.class);
	}
}
